package cn.udrm.elasticsearch.fulltext;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;

@Slf4j
public class FileOperation {

    public static void find(String fileTypes, String pathName, BuildFileIndex buildFileIndex,ExecutorService service,
                            List totalList,List list,String indexName,String docType) throws Exception {
        directRecure(fileTypes,pathName,buildFileIndex,service,totalList,list,indexName,docType);
    }

    public static void directRecure(String fileTypes, String pathName, BuildFileIndex buildFileIndex,
                                    ExecutorService service, List totalList,List list,String indexName,String docType){
        File file=new File(pathName);
        if(file.isDirectory()){
            String[] paths=file.list();
            for(String path:paths){
                String filePath=file.getPath()+File.separator+path;
                directRecure(fileTypes,filePath,buildFileIndex,service,totalList,list,indexName,docType);
            }
        }else{
            if(fileTypes.contains(getFileExt(file))){
                DirectoryRecurse directoryRecurse=new DirectoryRecurse(buildFileIndex,file,totalList,indexName,docType);
                service.submit(directoryRecurse);
                list.add(file);
            }
        }
    }

    private static String getFileExt(File file){
        String filename = file.getName();
        String[] strArray = filename.split("\\.");
        int suffixIndex = strArray.length - 1;
        return strArray[suffixIndex];
    }

}
